package org.xinjiang.tarim.travel.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xinjiang.tarim.travel.model.RouteDayPoi;
import org.xinjiang.tarim.travel.service.RouteDayPoiService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/routeDayPoi")
public class RouteDayPoiApi {
    @Autowired
    RouteDayPoiService routeDayPoiService;

    /**
     * 添加地点
     * @param param
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(@RequestBody String param){
        JsonResult jsonResult;
//        List list = JSON.parseObject(param, List.class);
        Map map = JSON.parseObject(param, Map.class);
        map = JSON.parseObject(map.get("param").toString(), Map.class);
        List list = JSONArray.parseArray(map.get("checkedList").toString(), RouteDayPoi.class);
        try {
            list = routeDayPoiService.insertByList(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "添加成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "添加异常");
        }
        return jsonResult;
    }

    /**
     * 添加地点
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    public JsonResult delete(Long id){
        JsonResult jsonResult;
        try {
            int i  = routeDayPoiService.delete(id);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

}
